<?php
namespace app\foreigndata\controller;

use app\common\controller\Api;
use think\Controller;

import('Aoplient',ALI_PATH);

/**
 * 首页接口
 */
class Alilogin extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];
    protected $appid = '2021002189601389';
//    protected $appsecret = 'dc185776d472eab6fa8db06b874a4b52';
    protected $Lifetime = 7200;

    public function Aliindex(){
        $url = urlencode("https://www.780g.com/foreigndata/Alilogin/back_url");
//        $url = "https://www.780g.com/foreigndata/Alilogin/back_url";
        header("Location:https://openauth.alipay.com/oauth2/publicAppAuthorize.htm?app_id=".$this->appid."&scope=auth_user&redirect_uri=".$url."&state=init");

    }

    /**
     * 从证书中提取公钥
     * @param $cert
     * @return mixed
     */
    public function getPublicKey($certPath)
    {
        $cert = file_get_contents($certPath);
        $pkey = openssl_pkey_get_public($cert);
        $keyData = openssl_pkey_get_details($pkey);
        $public_key = str_replace('-----BEGIN PUBLIC KEY-----', '', $keyData['key']);
        $public_key = trim(str_replace('-----END PUBLIC KEY-----', '', $public_key));
        return $public_key;
    }

    public function back_url(){
        // include_once realpath(__DIR__ ."/../../../extend/Alipay/alipay-sdk-PHP-4.9.2/aop/AopClient.php");
        // include_once realpath(__DIR__ ."/../../../extend/Alipay/alipay-sdk-PHP-4.9.2/aop/request/AlipaySystemOauthTokenRequest.php");
        $auth_code = $_GET['auth_code'];

        $aop = new \AopClient ();
        $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
        $aop->appId = $this->appid;
        $aop->rsaPrivateKey = 'MIIEpAIBAAKCAQEAu73O2zF+vjRZ10U03uWGKDhMkznNgj0G0A/iSItPvZTxtKZreUS0mNee5Ey0DcXcGYVxSnpslbpTIKivWMa5Oum0MwwRn4McZzhrlgmAZ1baEHuhxDHl1aTp+k6tvMOUuaFDUB75IzPLP+DF2k4ncL94ItZfNeW6Ifom3Xg2Zx7VMxXzv6aUMp+NZICajJ8M2PVbLW75OWqdIeGDhPoRu2fHeuCdGx626j+4b0kJYT2MFGH1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VsXTrbJ/0dnNkAr5U72VuCTwjEzrqozycOHNKPtJC1LWvfaSxyPwxXnXu8oLlb9Us3ffuta5Fm8QKBgQDNHXlNTEBISw9iJX0GgyPutCwrYD0/p2TKj/r/4+7pLHZOtK071MxLqYN0j06GF7MsHi4swUCCuZIR7Jv+X9XLju8mjAn/pXofZ3DVg347dIQmkhOJmPiTnjFfXL60Zoa6pX7f4qLJ6Dw9ddAJiy0pfLgA9E4hhxsxwvK2xPq1gg==';
        // $aop->alipayrsaPublicKey='请填写支付宝公钥，一行字符串';
        $aop->alipayrsaPublicKey      = $this->getPublicKey(CERT_PATH.'alipayCertPublicKey.crt');//调用getPublicKey从支付宝公钥证书中提取公钥
        $aop->apiVersion = '1.0';
        $aop->signType = 'RSA2';
        $aop->postCharset='GBK';
        $aop->format='json';
        $request = new \AlipaySystemOauthTokenRequest();
        $request->setGrantType("authorization_code");
        $request->setCode($auth_code);
        $result = $aop->execute ( $request);
        dump($result);
        $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
        $resultCode = $result->$responseNode->code;
        if(!empty($resultCode)&&$resultCode != 10000){
            echo "失败";
        } else {
            echo "成功";
        }

    }
}